﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using NewComer06.VisualWebPartNewComer;

namespace NewComer06.MVP
{
    public class NewComerDAO
    {

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static List<Comer> GetData()
        {

            List<Comer> _result = new List<Comer>();
            var dc = new SPLinqDataContext(SPContext.Current.Web.Url);

            var NewComers = dc.GetList<NewComer>("ListNewComer");

            var empQuery = (from emp in NewComers
                            orderby emp.StartDate descending
                            select new
                            {
                                emp.Id,
                                emp.FirstName,
                                emp.LastName,
                                emp.Jobtitle,
                                emp.Picture,
                            }).Take(3);

            foreach (var temp in empQuery)
            {
                Comer person = new Comer();
                person.Id = int.Parse(temp.Id.ToString());
                person.FirstName = temp.FirstName;
                person.LastName = temp.LastName;
                person.Jobtitle = temp.Jobtitle;
                person.Picture = temp.Picture;
                _result.Add(person);
            }
            return _result;
        }

        /// <summary>
        /// numers new comers in a page
        /// </summary>
        /// <returns></returns>
        public static List<Comer> GetDataAll(int n)
        {
            List<Comer> _result = new List<Comer>();
            var dc = new SPLinqDataContext(SPContext.Current.Web.Url);

            var NewComers = dc.GetList<NewComer>("ListNewComer");

            

            if (NewComers.Count() >= n)
            {
                var empQuery1 = (from emp in NewComers
                                 orderby emp.StartDate descending
                                 select emp
                           ).Take(n);

                foreach (var temp in empQuery1)
                {
                    Comer person = new Comer();
                    person.Id = int.Parse(temp.Id.ToString());
                    person.FirstName = temp.FirstName;
                    person.LastName = temp.LastName;
                    person.Jobtitle = temp.Jobtitle;
                    person.Project = temp.Project;
                    person.Reportto = temp.Reportto;
                    person.Skill = temp.Skill;
                    person.Source = temp.Source;
                    person.StartDate = (DateTime)temp.StartDate;
                    person.Picture = temp.Picture;

                    _result.Add(person);
                }
            }
            else
            {
                // if NewComers.Count() < n
                var empQuery = (from emp in NewComers
                                orderby emp.StartDate descending
                                select emp
                               );
                foreach (var temp in empQuery)
                {
                    Comer person = new Comer();
                    person.Id = int.Parse(temp.Id.ToString());
                    person.FirstName = temp.FirstName;
                    person.LastName = temp.LastName;
                    person.Jobtitle = temp.Jobtitle;
                    person.Project = temp.Project;
                    person.Reportto = temp.Reportto;
                    person.Skill = temp.Skill;
                    person.Source = temp.Source;
                    person.StartDate = (DateTime)temp.StartDate;
                    person.Picture = temp.Picture;

                    _result.Add(person);
                }
            }
            

           
            return _result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static Comer GetDataFollowId(int _id)
        {
            List<Comer> _result = new List<Comer>();
            var dc = new SPLinqDataContext(SPContext.Current.Web.Url);

            var NewComers = dc.GetList<NewComer>("ListNewComer");

            var empQuery = (from emp in NewComers
                            where emp.Id == _id
                            orderby emp.StartDate descending
                            select emp);

            Comer kq = (Comer)empQuery;
            
            Comer person = new Comer();
            person.Id = int.Parse(kq.Id.ToString());
            person.FirstName = kq.FirstName;
            person.LastName = kq.LastName;
            person.Jobtitle = kq.Jobtitle;
            person.Project = kq.Project;
            person.Reportto = kq.Reportto;
            person.Skill = kq.Skill;
            person.Source = kq.Source;
            person.StartDate = (DateTime)kq.StartDate;
            person.Picture = kq.Picture;


            return person;
        }
    }
}
